package com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.service.impl;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.constants.PlatformConstants;
import com.css.bpm.platform.sys.dict.repository.entity.DictItemEntity;
import com.css.bpm.platform.utils.PlatformDateUtils;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.bpm.platform.utils.UUIDGenerator;
import com.css.zfzx.sjcj.common.excel.handler.ActiveFaultVerifyBaseHandler;
import com.css.zfzx.sjcj.common.utils.EasyPoiExcelUtil;
import com.css.zfzx.sjcj.common.utils.UpdateUtil;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.repository.HddcB7VolcanicsvyprojecttableNativeRepository;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.repository.HddcB7VolcanicsvyprojecttableRepository;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.repository.entity.HddcB7VolcanicsvyprojecttableEntity;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.service.HddcB7VolcanicsvyprojecttableService;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.viewobjects.HddcB7VolcanicsvyprojecttablePageVO;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.viewobjects.HddcB7VolcanicsvyprojecttableQueryParams;
import com.css.zfzx.sjcj.modules.hddcB7_VolcanicSvyProjectTable.viewobjects.HddcB7VolcanicsvyprojecttableVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 火山调查数据-火山化学测量工程表管理
 * @author zhangcong
 * @date 2020-11-26
 */
@Service
public class HddcB7VolcanicsvyprojecttableServiceImpl implements HddcB7VolcanicsvyprojecttableService {

    private final HddcB7VolcanicsvyprojecttableRepository hddcB7VolcanicsvyprojecttableRepository;
    private final HddcB7VolcanicsvyprojecttableNativeRepository hddcB7VolcanicsvyprojecttableNativeRepository;
    private final ActiveFaultVerifyBaseHandler activeFaultVerifyBaseHandler;

    @Autowired
    public HddcB7VolcanicsvyprojecttableServiceImpl(HddcB7VolcanicsvyprojecttableRepository hddcB7VolcanicsvyprojecttableRepository,
                                                    HddcB7VolcanicsvyprojecttableNativeRepository hddcB7VolcanicsvyprojecttableNativeRepository,
                                                    ActiveFaultVerifyBaseHandler activeFaultVerifyBaseHandler) {
        this.hddcB7VolcanicsvyprojecttableRepository = hddcB7VolcanicsvyprojecttableRepository;
        this.hddcB7VolcanicsvyprojecttableNativeRepository = hddcB7VolcanicsvyprojecttableNativeRepository;
        this.activeFaultVerifyBaseHandler = activeFaultVerifyBaseHandler;
    }

    @Override
    public JSONObject queryHddcB7Volcanicsvyprojecttables(HddcB7VolcanicsvyprojecttableQueryParams queryParams, int curPage, int pageSize,String sort,String order) {
        Page<HddcB7VolcanicsvyprojecttablePageVO> hddcB7VolcanicsvyprojecttablePage = this.hddcB7VolcanicsvyprojecttableNativeRepository.queryHddcB7Volcanicsvyprojecttables(queryParams, curPage, pageSize, sort, order);
        return PlatformPageUtils.formatPageData(hddcB7VolcanicsvyprojecttablePage);
    }


    @Override
    public HddcB7VolcanicsvyprojecttableEntity getHddcB7Volcanicsvyprojecttable(String id) {
        return this.hddcB7VolcanicsvyprojecttableRepository.findById(id).orElse(null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcB7VolcanicsvyprojecttableEntity saveHddcB7Volcanicsvyprojecttable(HddcB7VolcanicsvyprojecttableEntity hddcB7Volcanicsvyprojecttable) {
        String uuid = UUIDGenerator.getUUID();
        hddcB7Volcanicsvyprojecttable.setUuid(uuid);
        hddcB7Volcanicsvyprojecttable.setCreateUser(PlatformSessionUtils.getUserId());
        Timestamp now = PlatformDateUtils.getCurrentTimestamp();
        hddcB7Volcanicsvyprojecttable.setCreateTime(now);
        hddcB7Volcanicsvyprojecttable.setUpdateTime(now);
        hddcB7Volcanicsvyprojecttable.setIsValid(PlatformConstants.Y);
        hddcB7Volcanicsvyprojecttable.setQualityinspectionStatus(PlatformConstants.N);
        hddcB7Volcanicsvyprojecttable.setExtends10(PlatformConstants.Y);
        this.hddcB7VolcanicsvyprojecttableRepository.save(hddcB7Volcanicsvyprojecttable);
        return hddcB7Volcanicsvyprojecttable;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public HddcB7VolcanicsvyprojecttableEntity updateHddcB7Volcanicsvyprojecttable(HddcB7VolcanicsvyprojecttableEntity hddcB7Volcanicsvyprojecttable) {
        HddcB7VolcanicsvyprojecttableEntity entity = hddcB7VolcanicsvyprojecttableRepository.findById(hddcB7Volcanicsvyprojecttable.getUuid()).get();
        UpdateUtil.copyNullProperties(entity,hddcB7Volcanicsvyprojecttable);
        hddcB7Volcanicsvyprojecttable.setUpdateUser(PlatformSessionUtils.getUserId());
        hddcB7Volcanicsvyprojecttable.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
        this.hddcB7VolcanicsvyprojecttableRepository.save(hddcB7Volcanicsvyprojecttable);
        return hddcB7Volcanicsvyprojecttable;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteHddcB7Volcanicsvyprojecttables(List<String> ids) {
        List<HddcB7VolcanicsvyprojecttableEntity> hddcB7VolcanicsvyprojecttableList = this.hddcB7VolcanicsvyprojecttableRepository.findAllById(ids);
        if(!PlatformObjectUtils.isEmpty(hddcB7VolcanicsvyprojecttableList) && hddcB7VolcanicsvyprojecttableList.size() > 0) {
            for(HddcB7VolcanicsvyprojecttableEntity hddcB7Volcanicsvyprojecttable : hddcB7VolcanicsvyprojecttableList) {
                hddcB7Volcanicsvyprojecttable.setIsValid(PlatformConstants.N);
                hddcB7Volcanicsvyprojecttable.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcB7Volcanicsvyprojecttable.setUpdateTime(new Date());
                this.hddcB7VolcanicsvyprojecttableRepository.save(hddcB7Volcanicsvyprojecttable);
            }
        }
    }

    @Override
    public List<DictItemEntity> getValidDictItemsByDictCode(String dictCode) {
        return PlatformAPI.getDictAPI().getValidDictItemsByDictCode(dictCode);
    }

    @Override
    public void exportFile(HddcB7VolcanicsvyprojecttableQueryParams queryParams, HttpServletResponse response) {
        List<HddcB7VolcanicsvyprojecttableEntity> yhDisasterEntities = hddcB7VolcanicsvyprojecttableNativeRepository.exportYhDisasters(queryParams);
        List<HddcB7VolcanicsvyprojecttableVO> list=new ArrayList<>();
        for (HddcB7VolcanicsvyprojecttableEntity entity:yhDisasterEntities) {
            HddcB7VolcanicsvyprojecttableVO yhDisasterVO=new HddcB7VolcanicsvyprojecttableVO();
            BeanUtils.copyProperties(entity,yhDisasterVO);
            list.add(yhDisasterVO);
        }
        EasyPoiExcelUtil.exportExcel(list,"火山化学测量工程表","火山化学测量工程表",
                HddcB7VolcanicsvyprojecttableVO.class,"火山化学测量工程表.xls",response);
    }

    @Override
    public String importExcel(MultipartFile file, HttpServletResponse response) {
        try {
            // 返回的消息
            StringBuilder returnMsg = new StringBuilder();
            // 导入的参数信息
            ImportParams params = new ImportParams();
            // 设置简析的第一行
            params.setHeadRows(1);
            // 是否需要校验
            params.setNeedVerify(true);
            params.setVerifyHandler(activeFaultVerifyBaseHandler);
            // 获取到Excel数据
            ExcelImportResult<HddcB7VolcanicsvyprojecttableVO> result
                    = ExcelImportUtil.importExcelMore(file.getInputStream(), HddcB7VolcanicsvyprojecttableVO.class, params);
            List<HddcB7VolcanicsvyprojecttableVO> list = result.getList();
            //保存
            saveDisasterList(list);
            if (result.isVerifyFail()) {
                // 校验返回失败行信息
                for (HddcB7VolcanicsvyprojecttableVO yhDisasterVO : result.getFailList()) {
                    returnMsg.append("第").append(yhDisasterVO.getRowNum()).append("行").append(yhDisasterVO.getErrorMsg()).append("<br/>");
                }
            }
            return returnMsg.append("成功导入").append(result.getList().size()).append("条记录").toString();
        } catch (Exception e) {
            return "导入失败，请检查数据正确性";
        }
    }
    /**
     * 批量存入库表并且返回错误数量
     */
    public void saveDisasterList(List<HddcB7VolcanicsvyprojecttableVO> list) {
        for (HddcB7VolcanicsvyprojecttableVO hddcB7VolcanicsvyprojecttableVO : list) {
            HddcB7VolcanicsvyprojecttableEntity yhDisasterEntity = new HddcB7VolcanicsvyprojecttableEntity();
            // 将VO数据映射拷贝到库表中
            BeanUtils.copyProperties(hddcB7VolcanicsvyprojecttableVO, yhDisasterEntity);
            saveHddcB7Volcanicsvyprojecttable(yhDisasterEntity);
        }
    }

    /**
     * 逻辑删除-根据项目id删除数据
     */
    @Override
    public void deleteByProjectId(List<String> projectIds) {
        List<HddcB7VolcanicsvyprojecttableEntity> hddcB7VolcanicsvyprojecttableList = this.hddcB7VolcanicsvyprojecttableRepository.queryHddcB7VolcanicsvyprojecttablesByProjectId(projectIds);
        if(!PlatformObjectUtils.isEmpty(hddcB7VolcanicsvyprojecttableList) && hddcB7VolcanicsvyprojecttableList.size() > 0) {
            for(HddcB7VolcanicsvyprojecttableEntity hddcB7Volcanicsvyprojecttable : hddcB7VolcanicsvyprojecttableList) {
                hddcB7Volcanicsvyprojecttable.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcB7Volcanicsvyprojecttable.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                hddcB7Volcanicsvyprojecttable.setIsValid(PlatformConstants.N);
                this.hddcB7VolcanicsvyprojecttableRepository.save(hddcB7Volcanicsvyprojecttable);
            }
        }
    }

    /**
     * 逻辑删除-根据任务id删除数据
     */
    @Override
    public void deleteByTaskId(List<String> taskIds) {
        List<HddcB7VolcanicsvyprojecttableEntity> hddcB7VolcanicsvyprojecttableList = this.hddcB7VolcanicsvyprojecttableRepository.queryHddcB7VolcanicsvyprojecttablesByTaskId(taskIds);
        if(!PlatformObjectUtils.isEmpty(hddcB7VolcanicsvyprojecttableList) && hddcB7VolcanicsvyprojecttableList.size() > 0) {
            for(HddcB7VolcanicsvyprojecttableEntity hddcB7Volcanicsvyprojecttable : hddcB7VolcanicsvyprojecttableList) {
                hddcB7Volcanicsvyprojecttable.setUpdateUser(PlatformSessionUtils.getUserId());
                hddcB7Volcanicsvyprojecttable.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                hddcB7Volcanicsvyprojecttable.setIsValid(PlatformConstants.N);
                this.hddcB7VolcanicsvyprojecttableRepository.save(hddcB7Volcanicsvyprojecttable);
            }
        }

    }
}
